package com.allwees.data.analysis.product.sqlquery;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.allwees.bs.core.model.product.vo.HotWords;
import com.allwees.core.common.util.json.JsonUtil;
import com.allwees.core.elasticsearch.query.QueryParams;
import com.allwees.core.elasticsearch.query.sql.EsSqlQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 *
 *
 * @author yangylsky
 * @since 2020-04-01
 *
 */
@Repository
public class SearchWordQuery {
	@Autowired
	private EsSqlQuery esSqlQuery;

	public List<HotWords> searchHotWords(Integer top) {
    	String json = esSqlQuery.query("product/HOT_WORDS.txt", QueryParams.of().put("top", top));

		JSONObject jsonObject = JsonUtil.toJSONObject(json);
		JSONArray jsonArr = (JSONArray) jsonObject.get("rows");
		List<HotWords> hotWords = new ArrayList<>();
		if(jsonArr != null) {
			jsonArr.forEach(item -> {
				JSONArray objs = (JSONArray) item;
				hotWords.add(new HotWords(objs.getString(0), objs.getLong(1)));
			});
		}
		return hotWords;
	}
}

